<?php 
class Page{
	public $total;     //总条数
	public $pagesize;  //每页的个数
	public $page;      //当前页
	public $pageall;   //总共有几页
	public $url;       //地址
	public $smartyObj; //smarty对象
	public function __construct($tatol,$pagesize,$smartyObj){		
		$this->total     = $tatol;
		$this->pagesize  = $pagesize;	
		$this->pageall   = ceil($this->total/$this->pagesize);	
		$this->setCurPage();
		$this->getCurUrl();
		$this->smartyObj = $smartyObj;
		$this->shrowPage();
		
	}	
	/**
	 *@todo 获得去除page=n后的地址
	 */
	public function getCurUrl(){		
		if(!empty($_SERVER["REQUEST_URI"])){
			$scriptName = $_SERVER["REQUEST_URI"];
			$nowurl = $scriptName;
		}else{
			$scriptName = $_SERVER["PHP_SELF"];
			if(empty($_SERVER["QUERY_STRING"])){
				$nowurl = $scriptName;
			}else{
				$nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
			}
		}	
		$nowurl = preg_replace("/\&page=.*&/",'&', $nowurl);
		$nowurl = preg_replace("/\?page=.*&/",'?', $nowurl);
		$nowurl = preg_replace("/\?page=.*/",'', $nowurl);
		$nowurl = preg_replace("/\&page=.*/",'', $nowurl);
		//$this->url = $nowurl;
		$this->url = preg_replace("/\&/",'&amp;',$nowurl);//$nowurl;
	}
	/**
	 * @todo 得到当前页码
	 * Enter description here ...
	 */
	public function setCurPage(){
		/*if(!empty($_SERVER["QUERY_STRING"])){
			$queryArray = explode("&",$_SERVER["QUERY_STRING"]);
			foreach ($queryArray as $val){
				$arr = explode("=",$val);
				if(isset($arr[0]) && ($arr[0] == 'page')){
					$pageNum = (isset($arr[1]) && (int)$arr[1]>1)?(int)$arr[1]:1;
					break;
				}
			}			
		}else{
			$pageNum = 1;
		}*/
		$pageNum    = isset($_GET['page'])?$_GET['page']:1;		
		$pageNum= min($pageNum,ceil($this->total/$this->pagesize));
		$this->page = $pageNum>0?$pageNum:1;
	}
	/**
	 * @todo 向前台抛出分页数据	
	 */
	public function shrowPage(){
		$this->smartyObj->assign('cur_page',$this->page);
		$this->assignUrl();
		$this->setPrePage();
		$this->setNextPage();
		$this->setAllPage();
		$this->getNumLink();
		$this->nowPage();
	}
	/**
	 * @todo assign 去掉page后的url
	 */
	function assignUrl(){
		$this->smartyObj->assign('nowUrl',$this->url);
	}
	/**
	 * @todo assign 当前页面
	 */
	function nowPage(){
		$this->smartyObj->assign('nowPage',$this->page);
	}
	/**
	 * @todo assign前一页
	 */
	function setPrePage(){
		$curentPage = $this->page;
		$this->getLink(2);		
		$this->smartyObj->assign('prePage',(($curentPage-1) > 0)?$this->getLink(($curentPage-1)):0);
	}
	/**
	 * @todo assign下一页
	 */
	function setNextPage(){
		$curentPage = $this->page;	
		$this->smartyObj->assign('nextPage',(($curentPage+1) <= $this->pageall)?$this->getLink(($curentPage+1)):0);
	}
	/**
	 * @todo assign 总页数
	 */
	function setAllPage(){
		$this->smartyObj->assign('allPage',$this->pageall);
	}
	/**
	 * @todo 返回页码是N的链接地址
	 * @param unknown_type $n
	 */
	public function getLink($n){
		if(count(explode("?",$this->url))>1){
			return $this->url."&amp;page=$n";
		}else{
			return $this->url."?page=$n";
		}
	}
	public function getNumLink(){
		$eachpage = array();
		if($this->pageall){
			for($i=0;$i<1;$i++){
				$eachpage[$i] = array('pagenum'=>$i+1,'link'=>$this->getLink($i+1));
			}
		}
		$this->smartyObj->assign('eachpage',$eachpage);
	}
}
?>